//index.js
//获取应用实例
const app = getApp()
let pageIndex = 1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fixed: "",
    active: [{
      active: "active",
      name: "流行",
      url: "http://list.meilishuo.com/search?frame=1&page=1&cKey=wap-index&tag=&maxPrice=&minPrice=&fcid=&_mgjuuid=24d569bd-3b44-4bff-8128-37ba8c1a2853&sort=pop&_=1531418196612&callback=jsonp13"
    }, {
      active: "",
      name: "新款",
      url: "http://list.meilishuo.com/search?frame=1&page=1&cKey=wap-index&tag=&maxPrice=&minPrice=&fcid=&_mgjuuid=24d569bd-3b44-4bff-8128-37ba8c1a2853&sort=new&_=1531418170259&callback=jsonp12"
    }, {
      active: "",
      name: "精选",
      url: "http://list.meilishuo.com/search?frame=1&page=1&cKey=wap-index&tag=&maxPrice=&minPrice=&fcid=&_mgjuuid=24d569bd-3b44-4bff-8128-37ba8c1a2853&sort=sell&_=1531417995598&callback=jsonp11"
    }],
    goodsList: [],
    hotHeight: 0,
    hotList: null
  },

  handleActive: function(event) {
    let url = "";
    this.data.active.map((item, index) => {
      if (index === event.currentTarget.dataset.index) {
        item.active = "active";
        url = item.url;
      } else {
        item.active = "";
      }
    });
    var self = this;
    wx.request({
      url: url,
      success: function(res) {
        var findex = res.data.indexOf("(");
        var lindex = res.data.indexOf(")");
        var res1 = JSON.parse(res.data.slice(findex + 1, lindex));
        // console.log(res1);
        self.setData({
          goodsList: res1.data.list,
          active: self.data.active
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var self = this;
    wx.request({
      url: 'http://list.meilishuo.com/search?frame=1&page=1&cKey=wap-index&tag=&maxPrice=&minPrice=&fcid=&_mgjuuid=24d569bd-3b44-4bff-8128-37ba8c1a2853&sort=pop&_=1531359763928&callback=jsonp8',
      success: function(res) {
        var findex = res.data.indexOf("(");
        var lindex = res.data.indexOf(")");
        var res1 = JSON.parse(res.data.slice(findex + 1, lindex));
        self.setData({
          goodsList: res1.data.list
        })
      }
    });


    //计算本周流行的每项的位置
    let hotTop = [17, 57];
    let hotLeft = 4;
    let hotList = [];
    hotTop.forEach(item => {
      hotLeft = 4;
      for(let i=1; i<=4; i++){
        hotLeft = (3 * i) + 21 * (i-1);
        hotList.push({
          left: hotLeft,
          top: item
        })
      }
    });
    let query = wx.createSelectorQuery();
    query.select("#singlebanner").boundingClientRect();
    query.exec(function(res){
      self.setData({
        hotHeight: res[0].height,
        hotList: hotList
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var self = this;
    wx.request({
      url: 'http://list.meilishuo.com/search?frame=3&cKey=wap-index&tag=&maxPrice=&minPrice=&fcid=&_mgjuuid=24d569bd-3b44-4bff-8128-37ba8c1a2853&sort=pop&trace=0&cpc_offset=0&_=1531417291846&callback=jsonp8',
      data: {
        "page": ++pageIndex
      },
      success: function(res) {
        var findex = res.data.indexOf("(");
        var lindex = res.data.indexOf(")");
        var res1 = JSON.parse(res.data.slice(findex + 1, lindex));
        // console.log(res1);
        var newgoodsList = self.data.goodsList.concat(res1.data.list);
        self.setData({
          goodsList: newgoodsList
        })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  onPageScroll: function(event) {
    if (event.scrollTop > 800){
      this.setData({
        fixed: "fixed"
      })
    }else{
      this.setData({
        fixed: ""
      })
    }
  },

  toDetail: function(){
    wx.navigateTo({
      url: '../detail/detail',
    })
  },
  handleSearch: function(){
    wx.navigateTo({
      url: '../search/search',
    })
  },
  zhuanHref: function(event){
    var link = event.currentTarget.dataset.href;
    wx.navigateTo({
      url: '../listSide/listSide?href='+link,
    })
  },
  naHotweek: function(){
    wx.navigateTo({
      url: '../hotWeek/hotWeek',
    })
  }
})